<?php
/*
	------
	-------      用户下单记录表
	------       Date:    2022.5.27
	-------      Author:  YSX  
*/
namespace app\admin\controller;

use app\common\controller\Backend;
use think\Db;
/**
 * 
 *
 * @icon fa fa-circle-o
 */
class SurprisedOrder extends Backend
{
    
    /**
     * CouponCodeOrderDetail模型对象
     * @var \app\admin\model\CouponCodeOrderDetail
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\SurprisedOrder;
        // $this->view->assign("statusLits",$this->model->statusLits());
    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 查看
     */
    public function index(){
        $this->relationSearch = true;

        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            //dump($where); exit;
            $total = $this->model
                ->where($where)
                ->order($sort, $order)
                ->count(); 
            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }


    public function detail($ids){
        $this->relationSearch = true;

        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $detail = Db::name('integral_order')->alias('a')
        	->where('a.id',$ids)
        	->leftJoin('areas b','a.province = b.id')
        	->leftJoin('areas c','a.city = c.id')
        	->leftJoin('areas d','a.county = d.id')
        	->field('a.*,b.name as province_name,c.name as city_name,d.name as county_name')
        	->find();
        // dump(Db::name('integral_order')->getLastSql());
        //查找商品信息
        $goodsInfo = Db::name('integral_order_goods')->where('order_id',$detail['id'])->select();
        // dump($detail);exit;
        $this->view->assign("row", $detail);
        $this->view->assign("goodsInfo", $goodsInfo);
        return $this->view->fetch();
    }
	public function deliver($ids){
		$row = $this->model->get($ids);
		if ($this->request->isPost()) {
			$params = $this->request->post("row/a");
			if ($params['logistics_name'] == '' || $params['express_number'] == '') {
				$this->error('参数不能为空！');
			}
			if ($params) {
			    $params = $this->preExcludeFields($params);
			    $result = false;
			    Db::startTrans();
			    try {
			        //先修改商品主表
			        $params['status'] = 3;
			        $result = Db::name('surprised_order')->where('id',$row['id'])->update($params);
			        Db::commit();
			    }catch (Exception $e) {
			        Db::rollback();
			        $this->error($e->getMessage());
			    }
			    if ($result !== false) {
			        $this->success();
			    } else {
			        $this->error(__('No rows were updated'));
			    }
			}
			$this->error(__('Parameter %s can not be empty', ''));
		}else{
			$detail = $this->model->find($ids);
			$this->view->assign("row", $detail);
			return $this->view->fetch();
		}
	}
}
